import TIM from 'tim-js-sdk';
import TIMUploadPlugin from 'tim-upload-plugin';

// 初始化 TIM SDK
const SDKAppID = '1400501212'; // 替换为你在腾讯云控制台获取的 SDKAppID
const tim = TIM.create({
	SDKAppID
});
// 存储消息接收回调函数，方便后续移除
let messageReceivedCallback = null;
// 存储 SDK_READY 事件的回调函数数组
let sdkReadyCallbacks = null;

// 注册上传插件
tim.registerPlugin({
	'tim-upload-plugin': TIMUploadPlugin
});

// 登录方法
const login = async (userID, userSig) => {
	try {
		await tim.login({
			userID,
			userSig
		});
		console.log('登录成功');
		return true;
	} catch (error) {
		console.error('登录失败', error);
		return false;
	}
};

// 退出登录方法
const logout = async () => {
	try {
		await tim.logout();
		if (messageReceivedCallback) {
			tim.off(TIM.EVENT.MESSAGE_RECEIVED, messageReceivedCallback);
			messageReceivedCallback = null;
		}
		if (sdkReadyCallbacks) {
			tim.off(TIM.EVENT.SDK_READY, sdkReadyCallbacks);
			sdkReadyCallbacks = null;
		}
		console.log('退出登录成功');
		return true;
	} catch (error) {
		console.error('退出登录失败', error);
		return false;
	}
};

// 发送文本消息方法
const sendTextMessage = async (toUserID, text) => {
	try {
		const message = tim.createTextMessage({
			to: toUserID,
			conversationType: TIM.TYPES.CONV_C2C,
			payload: {
				text
			}
		});
		const res = await tim.sendMessage(message);
		console.log('文本消息发送成功', res);
		return res;
	} catch (error) {
		console.error('文本消息发送失败', error);
		return null;
	}
};

// 发送图片消息方法
const sendImageMessage = async (toUserID, files) => {
	try {
		const message = tim.createImageMessage({
			to: toUserID,
			conversationType: TIM.TYPES.CONV_C2C,
			payload: {
				file:files
			}
		});
		const res = await tim.sendMessage(message);
		return res;
	} catch (error) {
		console.error('图片消息发送失败', error);
		return null;
	}
};

// 监听消息接收事件
const onMessageReceived = (callback) => {
	if (!messageReceivedCallback) {
		messageReceivedCallback = callback;
		tim.on(TIM.EVENT.MESSAGE_RECEIVED, messageReceivedCallback);
	}
};

// 拉取消息列表方法
const getMessageList = async (conversationID, lastMsgTime, count) => {
	try {
		const options = {
			conversationID,
			lastMsgTime,
			count
		};
		const res = await tim.getMessageList(options);
		return res.data.messageList;
	} catch (error) {
		console.error('消息列表拉取失败', error);
		return [];
	}
};

// 监听 SDK_READY 事件的公共方法
const onSDKReady = (callback) => {
	if (!sdkReadyCallbacks) {
		sdkReadyCallbacks = callback;
		// tim.on(TIM.EVENT.SDK_READY, () => {
		//     console.log('SDK 已准备好');
		//     sdkReadyCallbacks.forEach(cb => cb());
		// });
		tim.on(TIM.EVENT.SDK_READY, sdkReadyCallbacks);
	}
	// sdkReadyCallbacks.push(callback);
};

// 设置消息为已读方法
const setMessageRead = async (conversationID) => {
	try {
		const options = {
			conversationID
		};
		const res = await tim.setMessageRead(options);
		console.log('消息已标记为已读', res);
		return res;
	} catch (error) {
		console.error('标记消息为已读失败', error);
		return null;
	}
};

export {
	tim,
	login,
	logout,
	sendTextMessage,
	sendImageMessage,
	onMessageReceived,
	getMessageList,
	onSDKReady,
	setMessageRead
};